#include "stdafx.h"
#include "Debug.h"
#include "SyncObject.h"

static int g_nTraceLevel  = gnDbgLevelDetail;
static BOOL g_bInitialed = FALSE;
CRITICAL_SECTION g_csDebugTrace;


BOOL InitDebugTracing(int nLevel, int nMethod, BOOL bEnable, LPCTSTR lpszLogFile)
{
  if(g_bInitialed)
    return TRUE;

  g_nTraceLevel  = nLevel;

  ::InitializeCriticalSection(&g_csDebugTrace);
  DebugTracing(gnDbgLevelRun,_T("-------------------------- Output Debug Information --------------------------\n"));
  g_bInitialed = TRUE;
  return TRUE;
}

BOOL UninitDebugTracing()
{
  ::DeleteCriticalSection(&g_csDebugTrace);
  g_bInitialed = FALSE;
  return TRUE;
}

void __cdecl DebugTracing(int nLevel, LPCTSTR fmt, ... )
{
  CCritialSectionLock lock(&g_csDebugTrace);
/*
  if(nLevel > g_nTraceLevel)
  {
  }
*/
  LPTSTR lpszFinalMsg;
  TCHAR szMessage[512],szTemp[256];
  va_list va;
  va_start(va, fmt);
  wvsprintf(&szTemp[0], fmt, va);
  va_end(va);
  
  lpszFinalMsg = szTemp;
  wsprintf(szMessage,_T("(Process = %d, Thread = %d) -- %s"),(int)::GetCurrentProcessId(),(int)::GetCurrentThreadId(),szTemp);
  lpszFinalMsg = szMessage;
  lstrcat(lpszFinalMsg,_T("\n"));
  WZ_TRACE(lpszFinalMsg);
}
